package com.llx.controller;

import com.llx.entity.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @description
 * @author viki
 * @date 2021/10/24 17:57
 */
@Api(tags = "User")
@RestController
@RequestMapping("/users")
public class UserController {

    private static final List<User> USER_LIST = new ArrayList<>();

    static {
        USER_LIST.add(new User(1,"叶子",18));
        USER_LIST.add(new User(2,"大树",19));
        USER_LIST.add(new User(3,"海鸥",20));
    }

    @ApiOperation("新增用户")
    @PostMapping()
    public String addUser(@RequestBody  User user) {
        USER_LIST.add(user);
        return "success";
    }

    @ApiOperation("删除指定user")
    @ApiImplicitParam(name = "id",value = "user id",required = true,
            dataType = "Integer",paramType = "path")
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Integer id) {
        USER_LIST.remove(id - 1);
        return "success";
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "user id",required = true,
                    dataType = "Integer",paramType = "path"),
            @ApiImplicitParam(name = "user",value = "user实体",required = true,dataType = "User")
    })
    @ApiOperation("更新用户")
    @PutMapping("/{id}")
    public String editUser(@PathVariable Integer id,@RequestBody User user) {
        user.setId(id);
        USER_LIST.set(id - 1,user);
        return "success";
    }

    @GetMapping("/{id}")
    @ApiOperation("获取指定user")
    public User getUser(@PathVariable
                        @ApiParam(value = "id",required = true,defaultValue = "3") Integer id) {
        return USER_LIST.get(id - 1);
    }

    @GetMapping()
    @ApiOperation("获取用户列表")
    public List<User> getUserList() {
        return USER_LIST;
    }

    @ApiIgnore
    @GetMapping("ignore")
    public String ignoreTest() {
        return "ignore";
    }
}
